﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Models;
using DAL;
using Common;

namespace 包覆门.OperatorManager
{
    public partial class OperatorEditing : Form
    {
        public OperatorEditing()
        {
            InitializeComponent();
        }
        public bool IsNew { get; set; }
        public string EditingId { get; set; }
        private UserDAL dal = new UserDAL();

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ValidateNew())
            {
                int result = 0;
                if (IsNew)
                {
                    if (string.IsNullOrWhiteSpace(this.tbpassword.Text) && IsNew)
                    {
                        MessageBox.Show("密码不能为空");
                        return;

                    }
                    Operator o = new Operator();
                    o.realname = this.tbrealname.Text;
                    o.Username = this.tbusername.Text;
                    o.Password = this.tbpassword.Text;
                    o.phone = this.tbphone.Text;
                    o.AddTime = DateTime.Now.ToString();
                    o.EditTime = DateTime.Now.ToString();
                    o.AddUser = RemeberName.Username;
                    o.EditUser = RemeberName.Username;
                    foreach (RadioButton rb in this.gbType.Controls) {
                        if (rb.Checked) {
                            o.IsAdmin = rb.Tag.ToString();
                            break;
                        }
                    }
                     result = dal.SaveData(o);
                    if (result > 0)
                    {
                        DialogResult = DialogResult.OK;
                        MessageBox.Show("保存成功");

                    }
                }
                else {
                    Operator o = new Operator();
                    o.realname = this.tbrealname.Text;
                    o.Username = this.tbusername.Text;
                    o.Password = this.tbpassword.Text;
                    o.phone = this.tbphone.Text;
                    foreach (RadioButton rb in this.gbType.Controls)
                    {
                        if (rb.Checked)
                        {
                            o.IsAdmin = rb.Tag.ToString();
                            break;
                        }
                    }
                    o.EditTime = DateTime.Now.ToString();
                   
                    o.EditUser = RemeberName.Username;
                    o.Uid = Convert.ToInt32(EditingId);
                    if(string.IsNullOrWhiteSpace(o.Password)){
                         result = dal.UpdateByInfo(o, false);
                    }else{
                         result = dal.UpdateByInfo(o,true);
                    }
                    if (result>0)
                    {
                        DialogResult = DialogResult.OK;
                        MessageBox.Show("保存成功");
                    }
                }
            }
        }
        private bool ValidateNew() {
            bool flag = true;
            string username = this.tbusername.Text;
            string realname = this.tbrealname.Text;
            string phone=this.tbphone.Text;
            string password = this.tbpassword.Text;

            if (string.IsNullOrWhiteSpace(username)) {
                MessageBox.Show("账号不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(realname))
            {
                MessageBox.Show("姓名不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(phone))
            {
                MessageBox.Show("电话不能为空");
                return false;

            }
           

            return flag;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }

        private void OperatorEditing_Load(object sender, EventArgs e)
        {
            if (!IsNew) {
                LoadData(EditingId);
            }
        }
        private void LoadData(string id) {
            Operator o = dal.GetById(id);
            this.tbphone.Text = o.phone;
            this.tbrealname.Text = o.realname;
            this.tbusername.Text = o.Username;
            foreach(RadioButton rb in this.gbType.Controls){
                if (rb.Tag.ToString().Equals(o.IsAdmin))
                {
                    rb.Checked = true;
                }
                else {
                    rb.Checked = false;
                }
            }
        }
       

    }
}
